package com.itheima.controller;

import com.itheima.common.Paging;
import com.itheima.common.R;
import com.itheima.dto.NewsDto;
import com.itheima.service.NewsService;
import com.itheima.vo.NewsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <h1>消息功能控制类</h1>
 *
 * @author <a href="515857087@qq.com">叶世铭</a>
 * @since 2022/10/26
 */

@RestController
@RequestMapping("/news")
@Api(tags = "消息功能api")
public class NewsController {

    @Resource
    private NewsService newsService;

    /**
     * 展示与好友的最近20条聊天记录
     *
     * @param bUserId
     * @return
     */
    @ApiOperation(value = "展示与好友的最近20条聊天记录")
    @GetMapping("/showRecentNews")
    public R<List<NewsVo>> showRecentNews(@NotNull @RequestParam("userId") String bUserId) throws Exception {
        return R.success(newsService.showRecentNews(bUserId));
    }

    /**
     * 分页展示与好友的所有聊天记录
     *
     * @param newsDto
     * @return
     */
    @ApiOperation(value = "分页展示与好友的聊天记录，可按时间和关键字筛选")
    @PostMapping("/pageNews")
    public R<Paging<NewsVo>> pageNews(@RequestBody @Valid NewsDto newsDto) throws Exception {
        return R.success(newsService.pageNews(newsDto));
    }

    /**
     * 更新消息状态
     *
     * @param bUserId
     * @return
     */
    @ApiOperation(value = "更新消息状态")
    @PutMapping("/updateReadStatus")
    public R<String> updateReadStatus(@RequestParam("userId") String bUserId) throws Exception {
        newsService.updateReadStatus(bUserId);
        return R.success("状态更新成功");
    }

    /**
     * 撤回消息
     *
     * @param newsId
     * @return
     */
    @ApiOperation(value = "撤回消息")
    @DeleteMapping("/deleteNews")
    public R<String> deleteNews(Integer newsId) throws Exception {
        newsService.deleteNews(newsId);
        return R.success("已撤回");
    }
}
